﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Telerik.Web.Mvc;
using ThuySan.Bussiness;
using ThuySan.Models;

#endregion

namespace ThuySan.Areas.Backend.Controllers
{
    [Authorize]
    public class PropertyController : Controller
    {
        private readonly UnitOfWork _unitOfWork = new UnitOfWork();

        public ActionResult Index()
        {
            return View();
        }

        [GridAction]
        public ActionResult GetList()
        {
            return View(new GridModel(_unitOfWork.PropertyRepository.Select().Select(c => new {
                                                                                                      c.PropertyId,
                                                                                                      c.Name,
                                                                                                      c.GroupName,
                                                                                                      IsBit = c.IsBit ?? false
                                                                                                                                                     ? "checked"
                                                                                                                                                     : "",
                                                                                                      IsNumber = c.IsNumber ?? false
                                                                                                                         ? "checked"
                                                                                                                         : ""
                                                                                              })));
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            if (id != null)
            {
                string[] s = id.Split(',');
                int guid;
                List<int> list = (from s1 in s where int.TryParse(s1, out guid) select int.Parse(s1)).ToList();

                if (_unitOfWork.PropertyRepository.CheckDeleteProperty(list))
                {
                    _unitOfWork.PropertyRepository.Delete(list);
                }
            }
            return Content("");
        }

        public ActionResult Add()
        {
            return View(new PropertyModel());
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Add(PropertyModel propertyModel)
        {
            if (ModelState.IsValid)
            {
                Property property = propertyModel;
                if (_unitOfWork.PropertyRepository.CheckInsertProperty(property))
                {
                    Exception exception;
                    _unitOfWork.PropertyRepository.Insert(property, out exception);
                    if (exception == null)
                    {
                        return RedirectToAction("Index");
                    }
                    ModelState.AddModelError("error", exception.StackTrace);
                }
            }
            return View(propertyModel);
        }

        public ActionResult Edit(string id)
        {
            int guid;
            if (int.TryParse(id, out guid))
            {
                Property property = _unitOfWork.PropertyRepository.SelectbyId(guid);
                if (property != null)
                {
                    PropertyModel propertyModel = property;
                    return View(propertyModel);
                }
            }
            return RedirectToAction("Index");
        }

        [ValidateInput(false), HttpPost]
        public ActionResult Edit(PropertyModel propertyModel)
        {
            if (ModelState.IsValid)
            {
                Property property = _unitOfWork.PropertyRepository.SelectbyId(propertyModel.PropertyId);
                propertyModel.Update(property);
                if (_unitOfWork.PropertyRepository.CheckUpdateProperty(property))
                {
                    Exception exception;
                    _unitOfWork.PropertyRepository.Update(property, out exception);
                    if (exception == null)
                    {
                        return RedirectToAction("Index");
                    }
                    ModelState.AddModelError("error", exception.StackTrace);
                }
            }

            return View(propertyModel);
        }
    }
}
